package db

import (
	"context"
	"fmt"

	"v8s-controller/internal/configuration"

	"go.mongodb.org/mongo-driver/mongo"
	"go.mongodb.org/mongo-driver/mongo/options"
	"go.mongodb.org/mongo-driver/mongo/readpref"
)

type (
	MongoClient struct {
		*mongo.Client
	}
)

func CreateMongoClient(ctx context.Context, properties configuration.MongoProperties) *MongoClient {
	clientOptions := options.Client().ApplyURI(properties.ConnectionURI)

	client, err := mongo.Connect(ctx, clientOptions)
	if err != nil {
		panic(fmt.Errorf("couldn't create mongo client: %w", err))
	}

	err = client.Ping(ctx, readpref.Primary())
	if err != nil {
		panic(fmt.Errorf("couldn't ping mongodb: %w", err))
	}

	return &MongoClient{client}
}
